package study.ttl.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.ttl.common.Result;
import study.ttl.entity.Orders;
import study.ttl.service.OrdersService;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     * 参数：goodsId num type groupOrderId
     * @param orders
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Orders orders) {
        Orders savedOrder = ordersService.add(orders);
        return Result.success(savedOrder);
    }

    /**
     * 秒杀下单
     * @param orders
     * @return
     */
    @PostMapping("/addFlashOrder")
    public Result addFlashOrder(@RequestBody Orders orders) {
        Orders savedOrder = ordersService.addFlashOrder(orders);
        return Result.success(savedOrder);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @SaCheckRole("ADMIN")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        ordersService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @SaCheckRole("ADMIN")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改信息
     * @param orders
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Orders orders) {
        ordersService.update(orders);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Orders orders = ordersService.selectById(id);
        return Result.success(orders);
    }

    /**
     * 查询所有
     * @return
     */
    @SaCheckRole("ADMIN")
    @GetMapping("/selectAll")
    public Result selectAll(Orders orders) {
        List<Orders> ordersList = ordersService.selectAll(orders);
        return Result.success(ordersList);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param orders
     * @return
     */
    @SaCheckRole("ADMIN")
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Orders orders) {
        PageInfo<Orders> ordersList = ordersService.selectPage(pageNum, pageSize, orders);
        return Result.success(ordersList);
    }

    /**
     * 前台团购分页查询
     * @param pageNum
     * @param pageSize
     * @param orders
     * @return
     */
    @SaCheckRole("ADMIN")
    @GetMapping("/selectGroupPage")
    public Result selectGroupPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Orders orders) throws ParseException {
        PageInfo<Orders> ordersList = ordersService.selectGroupPage(pageNum, pageSize, orders);
        return Result.success(ordersList);
    }
}
